package com.otod.cd.doc.web;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.google.common.collect.Lists;
import com.otod.base.exception.AppException;
import com.otod.base.json.JsonSuccess;
import com.otod.cd.doc.bo.CdDocFolderRsultBo;
import com.otod.cd.doc.constants.ShareConstant;
import com.otod.cd.doc.service.CdDocFileService;
import com.otod.cd.doc.service.CdDocShareAuthService;
import com.otod.cd.doc.service.CdDocShareService;
import com.otod.cd.doc.vo.CdDocFolderVo;
import com.otod.commons.auth.SessionUserHolder;
import com.otod.commons.auth.SessionUserUtil;
import com.otod.commons.constants.CommonConstant;
import com.otod.commons.utils.StringUtils;
import com.otod.commons.web.BaseController;
import com.otod.pm.prj.service.ProjectService;
import com.otod.pm.prj.vo.ProjectVo;
import com.otod.sys.emp.service.IEmpService;
import com.otod.sys.role.service.IRoleService;
import com.otod.sys.vo.AuapTEmpVo;
import com.otod.sys.vo.AuapTRoleVo;



@Controller
@RequestMapping("cd/doc")
public class CdDocFileController extends BaseController {
	
	@Autowired	
	private CdDocFileService cdDocFileService;
	@Autowired
	private IEmpService empService;
	@Autowired
	private IRoleService roleService;
	@Autowired
	private ProjectService projectService;
	
	@Autowired	
	private CdDocShareService cdDocShareService; //分享
	
	@Autowired
	private CdDocShareAuthService cdDocShareAuthService;
	
	private static Logger logger = LoggerFactory.getLogger(CdDocFileController.class);
	
	
	


	
	/**
	 * 查询列表
	 * @param request
	 * @param parentId
	 * @param type
	 * @return
	 * 
	 * 添加了一个type类型  普通文档 项目文档查询用
	 */
	@RequestMapping(value="queryForList",method = RequestMethod.GET)
	@ResponseBody
	public CdDocFolderRsultBo queryForList(HttpServletRequest request,String parentId,String type){
		String isDel = CommonConstant.COMMON_NO;			//COMMON_NO 没有删除
		SessionUserHolder sessionUser = SessionUserUtil.getCurrentUser();
		String roleNo = sessionUser.getRoleNo();
		List<CdDocFolderVo> list = null;
		if("manager".equals(roleNo)||"mister".equals(roleNo)){
			
			 list = cdDocFileService.queryForListForParentId(parentId,isDel,type,null);
		}else{
			
			 list = cdDocFileService.queryForListForParentId(parentId,isDel,type,sessionUser.getUserNo());
		}
		
		CdDocFolderVo vo = new CdDocFolderVo();
		if(!StringUtils.isEmpty(parentId)){
			vo = cdDocFileService.getById(parentId);
		}
		CdDocFolderRsultBo bo = new CdDocFolderRsultBo();
		bo.setCdDocFolderVo(vo);
		bo.setChildrenList(list);
		return bo;
	}
	
	/**
	 * 保存文件夹
	 * @param folderName 文档名字
	 * @throws AppException 
	 * 
	 *  添加了一个type类型  普通文档 项目文档查询用
	 */
	@RequestMapping(value="saveFolder",method = RequestMethod.GET)
	@ResponseBody
	public String saveFolder(String parentId,String folderName,String type) throws AppException{			
		CdDocFolderVo cdDocFolderVo = new CdDocFolderVo();
		
		//解决如果是空值 向数据库插入null的问题
		if(StringUtils.isEmpty(parentId)){
			cdDocFolderVo.setParentId(null);
		}else{
			cdDocFolderVo.setParentId(parentId);
		}
		if(StringUtils.isEmpty(folderName)){
			cdDocFolderVo.setFolderName(null);
		}else{
			cdDocFolderVo.setFolderName(folderName);
		}
		
		cdDocFolderVo.setFolderType(CommonConstant.FOLDER);
		
		SessionUserHolder sessionUser = SessionUserUtil.getCurrentUser();
		cdDocFolderVo.setCreateEmpNo(sessionUser.getUserNo());
		
		cdDocFolderVo.setType(type);
		
		//添加用户
		super.setInsertOperInfo(cdDocFolderVo);
		//通过id查询数据
		cdDocFileService.saveFolder(cdDocFolderVo);
		return JsonSuccess.factory("添加成功");
	}
	/**
	 * 删除文件或者文件夹  是假删
	 * @param request
	 * @param parentId
	 * @return
	 */
	@RequestMapping(value="deleteFolder",method = RequestMethod.GET)
	@ResponseBody
	public String deleteFolder(HttpServletRequest request,String parentId){
		 String[] split = parentId.split(",");
		 cdDocFileService.deleteFolder(split);
		 return JsonSuccess.factory("删除成功");
	}
	/**
	 * 文件文件夹重命名
	 * @param uuid
	 * @param folderName
	 * @return
	 * @throws AppException 
	 */
	@RequestMapping(value="updateFolderName",method = RequestMethod.GET)
	@ResponseBody
	public String updateFolderName(String parentId,String uuid,String folderName) throws AppException{
		SessionUserHolder sessionUser = SessionUserUtil.getCurrentUser();
		cdDocFileService.updateFolderName(parentId,uuid,folderName,sessionUser.getUserNo());
		 return JsonSuccess.factory("修改成功");
	}
	
	/**********************************回收站**********************************************/
	/**
	 * 查询回收站的信息
	 * @return
	 */
	@RequestMapping(value="queryForListRecycle",method = RequestMethod.GET)
	@ResponseBody
	public List<CdDocFolderVo> queryForListRecycle(){
		List<CdDocFolderVo> recycleList = new ArrayList<CdDocFolderVo>();
		//得到删除的数据源
		String delSource = CommonConstant.COMMON_YES;
		SessionUserHolder sessionUser = SessionUserUtil.getCurrentUser();
		recycleList=cdDocFileService.queryForListRecycle(delSource,sessionUser.getUserNo());
		return recycleList;
	}
	/**
	 * 还原文件
	 * @param uuid
	 * @return
	 * @throws AppException 
	 */
	@RequestMapping(value="updatedelSource",method = RequestMethod.GET)
	@ResponseBody
	public String updatedelSource(String uuid) throws AppException{
		
		//判断是否重复 需要名字 和 上级目录的id
		SessionUserHolder sessionUser = SessionUserUtil.getCurrentUser();
		String repetitionFolderName=cdDocFileService.updatedelSource(uuid,sessionUser.getUserNo());
		//return repetitionFolderName;
		return JsonSuccess.factory(repetitionFolderName);
	}
	/**
	 * 清空回收站
	 * @return
	 */
	@RequestMapping(value="emptyRecyCle",method =RequestMethod.GET)
	@ResponseBody
	public String emptyRecyCle(){
		SessionUserHolder sessionUser = SessionUserUtil.getCurrentUser();
		cdDocFileService.emptyRecyCle(sessionUser.getUserNo());
		return JsonSuccess.factory("清空回收站成功");
	}
	/**
	 * 删除选中的的文件
	 */
	@RequestMapping(value="deleteFileUuid",method =RequestMethod.GET)
	@ResponseBody
	public String deleteFileUuid(String uuid){
		
		String[] splitUuid = uuid.split(",");
		SessionUserHolder sessionUser = SessionUserUtil.getCurrentUser();
		cdDocFileService.deleteFileUuid(splitUuid, sessionUser.getUserNo());
		return JsonSuccess.factory("删除文件成功");
	}
	
	/*********************************分享********************************************/
	

	
	
	/**
	 * 查询所有人员去掉当用户  这里post不能修改
	 * @return
	 */
	@RequestMapping(value="selectPerson",method =RequestMethod.POST)
	@ResponseBody
	public List<AuapTEmpVo> selectPerson(String id){
		
		List<AuapTEmpVo> empList = empService.findAll();
		List<AuapTEmpVo> auapTEmpVos = Lists.newArrayList();
		SessionUserHolder sessionUser = SessionUserUtil.getCurrentUser();
		for (AuapTEmpVo auapTEmpVo : empList) {
			if(!auapTEmpVo.getEmpNo().equals(sessionUser.getUserNo())){
				auapTEmpVos.add(auapTEmpVo);
			}
			
		}
		
		return auapTEmpVos;
	}
	/**
	 * 查询所有角色信息 这里post不能修改
	 * @return
	 */
	@RequestMapping(value="selectRole",method =RequestMethod.POST)
	@ResponseBody
	public List<AuapTRoleVo> selectRole(){
		List<AuapTRoleVo> roleVos = roleService.findAll();
		return roleVos;
	}
	/**
	 *  查询所有项目  没有考虑其他任何情况 这里post不能修改
	 * @return
	 */
	@RequestMapping(value="selectProjcet",method =RequestMethod.POST)
	@ResponseBody
	public List<ProjectVo> selectProjcet(){
		
		List<ProjectVo> projectVos = projectService.findAll();
		return projectVos;
	}
	
	/*
	*//**
	 * 私密 分享 给个人
	 * @param uuid
	 * @param person
	 * @return
	 *//*
	@RequestMapping(value="sharePerson",method =RequestMethod.GET)
	@ResponseBody
	public String sharePerson(String uuid,String person){
		cdDocShareService.sharePerson(uuid,person);
		return JsonSuccess.factory("分享成功");
	}*/
/********************************************************************************/	
/**************************私密分享 给角色***********/
	/**
	 * 私密分享给角色 要去掉当期用户
	 * @param uuid
	 * @param role
	 * @return
	 */
	/*@RequestMapping(value="privateShareRole",method = RequestMethod.GET)
	@ResponseBody
	public String privateShareRole(String uuid,String role){
		
		cdDocShareService.privateShareRole(uuid,role);
		
		return JsonSuccess.factory("分享成功");
		
	}*/

	
/****************私密分享项目***************************************/
	/**
	 * 私密分享项目
	 * @param uuid
	 * @param project
	 * @return
	 */
	/*@RequestMapping(value="privateProject",method = RequestMethod.GET)
	@ResponseBody
	public String privateProject(String uuid,String project){
		
		cdDocShareService.privateProject(uuid,project);
		return JsonSuccess.factory("分享成功");
	}*/
	
	
	/********************************保存修改分享**********************************************/
	/**
	 * 修改 
	 * @param allPRP 人员 角色 项目 的所有id
	 * @return
	 */
	@RequestMapping(value="updateShare",method = RequestMethod.GET)
	@ResponseBody
	public String updateShare(String person,String role,String project,String shareType, String uuid){
		//修改分享要先删除原来的数据 在插入新数据
		//需要前台传入 选中的人员 角色 项目 和文件夹id

		
		//新增数据
		SessionUserHolder sessionUser = SessionUserUtil.getCurrentUser();
		cdDocShareService.addPrivatePersonRolePrject(person,role,project,shareType,uuid,sessionUser.getUserNo());
		return JsonSuccess.factory("修改分享成功");
	}
	
	
	
	/**
	 * 公共分享
	 * @param uuid 分享文件 或文件夹得id   
	 * @return
	 */
	@RequestMapping(value="publicShare",method =RequestMethod.GET)
	@ResponseBody
	public String publicShare(String uuid){
		
		cdDocShareService.saveShare(uuid);
		return JsonSuccess.factory("分享成功");
	}
	
	/**
	 * 查询某个项目下的所有文件  文件夹
	 * @param type  文库类型
	 * @param uuid  项目id
	 * @return
	 */
	@RequestMapping(value="queryProjectFile",method =RequestMethod.GET)
	@ResponseBody
	public List<CdDocFolderVo> queryProjectFile(String type,String uuid){
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("uuidOrFolderPath", uuid);
		map.put("type", type);
		List<CdDocFolderVo> cdDocFolderVos = Lists.newArrayList();
		cdDocFolderVos = cdDocFileService.queryForList(map);
		return cdDocFolderVos;
	}

	
	/**
	 * 移动到公司文库
	 * @param uuid
	 * @return
	 * @throws AppException 
	 */
	@RequestMapping(value="moveFrimLibrary",method =RequestMethod.GET)
	@ResponseBody
	public String moveFrimLibrary(String uuid) throws AppException{
		cdDocFileService.moveFrimLibrary(uuid);
		return JsonSuccess.factory("移动成功");
	}
	
}
